//Definindo ...
package fmu.pi.view.draw.old;

import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.VBox; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.GridPane; 
import javafx.scene.control.Label; 
import javafx.scene.control.Button; 
import javafx.scene.text.Font; 
import javafx.scene.chart.LineChart; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import java.util.Calendar; 

import fmu.pi.view.handler.HandlerView;
import javafx.geometry.Pos;
import javafx.scene.chart.CategoryAxis;

public class Home extends BorderPane
{
    private Button ascButton; 
    private Button descButton; 
    private Label saldoLabel;
    private Label yearLabel;
    private Integer year; 
    private LineChart lineChart;
    private HandlerView handler; 
    
    public Home()
    {
        initPane(); 
    }
    
    private void initPane()
    {
        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR); //ano atual
        handler = new HandlerView(); 
        
        setCenter(graficPane());  
    }
    
    private GridPane graficPane()
    {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        
        HBox box = new HBox(); 
        box.setSpacing(10);
        box.setAlignment(Pos.CENTER);
        
        ascButton = new Button();
        ascButton.setText(" + "); 
        ascButton.setMaxSize(50, 30);
        ascButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                ++year; 
                yearLabel.setText(String.valueOf(year)); 
                setCenter(graficPane()); 
            }
        });
        
        descButton = new Button(); 
        descButton.setText(" - ");
        descButton.setMaxSize(50, 30);
        descButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event) 
            {
                --year;
                yearLabel.setText(String.valueOf(year)); 
                setCenter(graficPane()); 
            }
        });
        
        yearLabel = new Label(); 
        yearLabel.setText(String.valueOf(year));
        yearLabel.setFont(Font.font("Ariel Black", 20));
        box.getChildren().addAll(descButton, yearLabel, ascButton); 
        
        lineChart = createChart(String.valueOf(year)); 
        
        grid.add(box, 0, 0); 
        grid.add(lineChart, 0, 1); 
        
        
        return grid; 
    }
    
    //cria grafico 
    private LineChart createChart(String year)
    {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis(); 
        xAxis.setLabel("Orçamento 2012"); 
        
        LineChart chart = new LineChart<String, Number>(xAxis, yAxis);
        chart.setMaxSize(500, 500); 
        chart.setTitle("Balanço Anual"); 
        
        XYChart.Series series = new XYChart.Series();
        series.setName("Saldo");
         
        
        series.getData().add(new XYChart.Data("Jan", handler.getSaldo("31/01/"+year)));
        series.getData().add(new XYChart.Data("Fev", handler.getSaldo("29/02/"+year)));
        series.getData().add(new XYChart.Data("Mar", handler.getSaldo("31/03/"+year)));
        series.getData().add(new XYChart.Data("Abr", handler.getSaldo("30/04/"+year)));  
        series.getData().add(new XYChart.Data("Mai", handler.getSaldo("31/05/"+year)));
        series.getData().add(new XYChart.Data("Jun", handler.getSaldo("30/06/"+year)));
        series.getData().add(new XYChart.Data("Jul", handler.getSaldo("31/07/"+year)));
        series.getData().add(new XYChart.Data("Ago", handler.getSaldo("31/08/"+year)));
        series.getData().add(new XYChart.Data("Set", handler.getSaldo("30/09/"+year)));
        series.getData().add(new XYChart.Data("Out", handler.getSaldo("31/10/"+year)));
        series.getData().add(new XYChart.Data("Nov", handler.getSaldo("30/11/"+year)));
        series.getData().add(new XYChart.Data("Dez", handler.getSaldo("31/12/"+year)));
        chart.getData().add(series);

        
        return chart; 
    }
}
